<template>
  <el-cascader :value="value" placeholder="请选择" separator="-" size="mini" :options="treeData" :props="props" @change="changeValue" />
</template>
<script>
import { getDepartmentList } from '@/api/department'
import { transformListToTreeData } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.treeData = transformListToTreeData(await getDepartmentList(), 0)
    },
    changeValue(list) {
      if (list.length) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
